package com.bjb.vr.auth.controller;

import com.bjb.vr.auth.service.UmsOptAccountEnhanceService;
import com.bjb.vr.common.ao.*;
import com.bjb.vr.common.constant.ThreadLocalConstant;
import com.bjb.vr.common.dto.UmsAccount;
import com.bjb.vr.common.dto.UmsOptAccount;
import com.bjb.vr.common.dto.UmsOptRole;
import com.bjb.vr.common.dto.UmsRole;
import com.bjb.vr.common.result.BaseErrorCode;
import com.bjb.vr.common.result.BaseResult;
import com.bjb.vr.common.result.CommonResult;
import com.bjb.vr.common.service.UmsOptAccountService;
import com.bjb.vr.common.service.UmsOptUserRoleService;
import com.bjb.vr.common.service.UmsUserRoleService;
import com.bjb.vr.common.utils.ThreadLocalUtil;
import com.bjb.vr.common.vo.UmsRoleVo;
import com.bjb.vr.common.vo.UmsUserRoleVo;
import io.jsonwebtoken.lang.Assert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * 运营端用户管理列表
 *
 * @description:
 * @author: HuJingBo
 * @time: 2022/11/17 10:37
 */
@Slf4j
@RestController
@RequestMapping("/api/auth/v1")
public class OptAccountController {

	@Resource
	private UmsOptAccountService optAccountService;

	@Resource
	private UmsOptUserRoleService optUserRoleService;

	@Resource
	private UmsOptAccountEnhanceService optAccountEnhanceService;


	/**
	 * 查询用户信息
	 *
	 * @return
	 */
	@PostMapping("/queryOptUser")
	public CommonResult queryOptUser(@Valid @RequestBody UmsUserRoleAo umsUserRoleAo) {
		List<UmsUserRoleVo> userRoleVos = optUserRoleService.queryUserRoleInfo(umsUserRoleAo);
		return CommonResult.Ok().setResult(userRoleVos);
	}

	/**
	 * 新增用户
	 *
	 * @param umsAccountAo
	 * @return
	 */
	@PostMapping("/addOptUser")
	public CommonResult addUser(@RequestBody UmsAccountAo umsAccountAo) {
		// 查询用户名或者昵称是否存在
		List<UmsOptAccount> optAccounts = optAccountService.queryUserInfoByNameOrNickName(umsAccountAo);
		if (!CollectionUtils.isEmpty(optAccounts)){
			return CommonResult.Fail("用户账号或者姓名已存在");
		}
		// 新增用户
		boolean success = optAccountService.saveUser(umsAccountAo);
		if (!success) {
			return CommonResult.Fail(BaseErrorCode.ERROR_ADD);
		}
		return CommonResult.Ok();
	}

	/**
	 * 更新用户角色
	 *
	 * @param umsAccountAo
	 * @return
	 */
	@PostMapping("/updateOptUser")
	public CommonResult updateUser(@RequestBody UmsAccountAo umsAccountAo) {
		Assert.notNull(umsAccountAo.getUserId(), "用户ID不能为空");
		boolean success = optAccountEnhanceService.updateUserInfo(umsAccountAo);
		if (!success) {
			return CommonResult.Fail(BaseErrorCode.ERROR_UPDATE);
		}
		return CommonResult.Ok();
	}

	/**
	 * 重置用户密码
	 *
	 * @param accountPwdAo
	 * @return
	 */
	@PostMapping("/resetOptUserPassword")
	public BaseResult updateUserPassword(@RequestBody UmsAccountPwdAo accountPwdAo) {
		Assert.notNull(accountPwdAo.getPassword(), "新密码不能为空");
		// 修改密码
		boolean success = optAccountEnhanceService.updateOptUserPassword(accountPwdAo);
		if (!success) {
			return CommonResult.Fail(BaseErrorCode.ERROR_UPDATE);
		}
		return CommonResult.Ok();
	}

	/**
	 * 更新用户状态
	 *
	 * @param statusAo
	 * @return
	 */
	@PostMapping("/updateOptUserStatus")
	public CommonResult updateUserStatus(@RequestBody UmsAccountStatusAo statusAo) {
		Assert.notNull(statusAo.getId(), "用户ID不能为空");
		boolean success = optAccountEnhanceService.updateUserStatus(statusAo);
		if (!success) {
			return CommonResult.Fail(BaseErrorCode.ERROR_UPDATE);
		}
		return CommonResult.Ok();
	}

	/**
	 * 查询角色信息
	 *
	 * @return
	 */
	@PostMapping("/queryOptRole")
	public CommonResult queryRole(@RequestBody UmsUserRoleAo userRoleAo) {
		ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, true);
		List<UmsRoleVo> umsRoleVos = optAccountEnhanceService.queryRoleInfo(userRoleAo);
		return CommonResult.Ok().setResult(umsRoleVos);
	}

	/**
	 * 添加角色
	 *
	 * @return
	 */
	@PostMapping("/addOptRole")
	public CommonResult addRole(@RequestBody UmsRoleAo umsRoleAo) {
		Assert.notNull(umsRoleAo.getRoleName(), "角色名称不能为空");
		Assert.notNull(umsRoleAo.getMenuIds(), "菜单ID不能为空");
		Assert.notNull(umsRoleAo.getType(), "菜单类型不能为空");
		// 查询角色名称是否存在
		UmsOptRole umsOptRole = optAccountEnhanceService.queryRoleByName(umsRoleAo.getRoleName());
		if (Objects.nonNull(umsOptRole)){
			return CommonResult.Fail("角色名称已存在");
		}
		// 添加角色
		boolean success = optAccountEnhanceService.addRoleInfo(umsRoleAo);
		if (!success) {
			return CommonResult.Fail(BaseErrorCode.ERROR_ADD);
		}
		return CommonResult.Ok("新增成功");
	}

	/**
	 * 更新角色
	 *
	 * @return
	 */
	@PostMapping("/updateOptRole")
	public CommonResult updateRole(@RequestBody UmsRoleAo umsRoleAo) {
		Assert.notNull(umsRoleAo.getRoleId(), "角色ID不能为空");
		boolean success = optAccountEnhanceService.updateRole(umsRoleAo);
		if (!success) {
			return CommonResult.Fail(BaseErrorCode.ERROR_ADD);
		}
		return CommonResult.Ok("更新成功");
	}
}
